"""
单例模式：保证在程序中多次实例化只会产生同一个对象，目的是为了减少创建对象与销毁对象这个过程损耗的计算机资源。
使用场景：日志，商城购物车
"""
import time
from threading import Thread


class Singleton(object):
    __instance = None

    def __new__(cls, *args, **kwargs):
        if cls.__instance is None:
            # 1. 代码量多了，遇到阻塞了
            # 2. 计算机资源有限
            time.sleep(0.01)
            cls.__instance = super().__new__(cls)
        return cls.__instance


class Humen(Singleton):
    def __init__(self, name, age):
        self.name = name
        self.age = age


def create_perple():
    xm = Humen("xiaoming", 20)
    print(xm)


if __name__ == '__main__':
    for i in range(100):
        thread = Thread(target=create_perple)
        thread.start()
